{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![AWS SDK for pandas](_static/logo.png \"AWS SDK for pandas\")](https://github.com/aws/aws-sdk-pandas)\n",
    "\n",
    "# 7 - Redshift, MySQL, PostgreSQL, SQL Server and Oracle\n",
    "\n",
    "[awswrangler](https://github.com/aws/aws-sdk-pandas)'s Redshift, MySQL and PostgreSQL have two basic functions in common that try to follow Pandas conventions, but add more data type consistency.\n",
    "\n",
    "- [wr.redshift.to_sql()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.redshift.to_sql.html)\n",
    "- [wr.redshift.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.redshift.read_sql_query.html)\n",
    "- [wr.mysql.to_sql()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.mysql.to_sql.html)\n",
    "- [wr.mysql.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.mysql.read_sql_query.html)\n",
    "- [wr.postgresql.to_sql()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.postgresql.to_sql.html)\n",
    "- [wr.postgresql.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.postgresql.read_sql_query.html)\n",
    "- [wr.sqlserver.to_sql()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.sqlserver.to_sql.html)\n",
    "- [wr.sqlserver.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.sqlserver.read_sql_query.html)\n",
    "- [wr.oracle.to_sql()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.oracle.to_sql.html)\n",
    "- [wr.oracle.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.oracle.read_sql_query.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install the optional modules first\n",
    "!pip install 'awswrangler[redshift, postgres, mysql, sqlserver, oracle]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import awswrangler as wr\n",
    "\n",
    "df = pd.DataFrame({\"id\": [1, 2], \"name\": [\"foo\", \"boo\"]})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect using the Glue Catalog Connections\n",
    "\n",
    "- [wr.redshift.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.redshift.connect.html)\n",
    "- [wr.mysql.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.mysql.connect.html)\n",
    "- [wr.postgresql.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.postgresql.connect.html)\n",
    "- [wr.sqlserver.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.sqlserver.connect.html)\n",
    "- [wr.oracle.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.oracle.connect.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "con_redshift = wr.redshift.connect(\"aws-sdk-pandas-redshift\")\n",
    "con_mysql = wr.mysql.connect(\"aws-sdk-pandas-mysql\")\n",
    "con_postgresql = wr.postgresql.connect(\"aws-sdk-pandas-postgresql\")\n",
    "con_sqlserver = wr.sqlserver.connect(\"aws-sdk-pandas-sqlserver\")\n",
    "con_oracle = wr.oracle.connect(\"aws-sdk-pandas-oracle\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Raw SQL queries (No Pandas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n"
     ]
    }
   ],
   "source": [
    "with con_redshift.cursor() as cursor:\n",
    "    for row in cursor.execute(\"SELECT 1\"):\n",
    "        print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading data to Database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "wr.redshift.to_sql(df, con_redshift, schema=\"public\", table=\"tutorial\", mode=\"overwrite\")\n",
    "wr.mysql.to_sql(df, con_mysql, schema=\"test\", table=\"tutorial\", mode=\"overwrite\")\n",
    "wr.postgresql.to_sql(df, con_postgresql, schema=\"public\", table=\"tutorial\", mode=\"overwrite\")\n",
    "wr.sqlserver.to_sql(df, con_sqlserver, schema=\"dbo\", table=\"tutorial\", mode=\"overwrite\")\n",
    "wr.oracle.to_sql(df, con_oracle, schema=\"test\", table=\"tutorial\", mode=\"overwrite\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unloading data from Database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>foo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>boo</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id name\n",
       "0   1  foo\n",
       "1   2  boo"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wr.redshift.read_sql_query(\"SELECT * FROM public.tutorial\", con=con_redshift)\n",
    "wr.mysql.read_sql_query(\"SELECT * FROM test.tutorial\", con=con_mysql)\n",
    "wr.postgresql.read_sql_query(\"SELECT * FROM public.tutorial\", con=con_postgresql)\n",
    "wr.sqlserver.read_sql_query(\"SELECT * FROM dbo.tutorial\", con=con_sqlserver)\n",
    "wr.oracle.read_sql_query(\"SELECT * FROM test.tutorial\", con=con_oracle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "con_redshift.close()\n",
    "con_mysql.close()\n",
    "con_postgresql.close()\n",
    "con_sqlserver.close()\n",
    "con_oracle.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "awswrangler-v9JnknIF-py3.8",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5 (default, Apr 13 2022, 19:37:23) \n[Clang 13.0.0 (clang-1300.0.27.3)]"
  },
  "vscode": {
   "interpreter": {
    "hash": "83297b058d59ee0acd247586c837429190a8258f15c0eea6234359f5557dde51"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
